package net.hubt.itc.data;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class BasicXMLParser {
	// <name>abc</name>
	// getTagValue("name",e) -> abc
	public String getTagValue(String tag, Element element) {
		NodeList nl = element.getElementsByTagName(tag);
		if (nl.getLength() > 0) {
			Node node = nl.item(0);
			NodeList subNodeList = node.getChildNodes();
			if (subNodeList.getLength() > 0) {
				Node nValue = (Node) subNodeList.item(0);
				if (nValue != null && nValue.getNodeType() == Node.TEXT_NODE) {
					return nValue.getNodeValue();
				}
			}
		}
		return "";
	}
	
	/**
	 * <root>
	 * 		<node1>value</node1>
	 * 		<node2>value2</node2>
	 * 		<node3>
	 * 			<node3-1>value3-1</node3-1>
	 * 		</node3> 		
	 * </root>
	 * get text value of <node1>, <node2>
	 * @param tag
	 * @param element the root node (document node) = Document.getDocumentElement()
	 * @return
	 */
	public String getTagValueLevel1(String tag, Element element){
		if(element!=null){
			if(element.getChildNodes().getLength()>0){
				return getTagValue(tag, element);
			}			
		}
		return null;
	}
}
